/*******************************************************************
 *   > File Name: cmd_parse.c
 *   > Author: fly
 *   > Mail: XXXXXXXX@icode.com
 *   > Create Time: 2020年06月07日 星期日 10时19分16秒
 ******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cmd_parse.h"

void led_on_process(void){
    printf("%s\n",__FUNCTION__);
}

void led_off_process(void){
    printf("%s\n",__FUNCTION__);
}

void motor_on_process(void){
    printf("%s\n",__FUNCTION__);
}

void motor_off_process(void){
    printf("%s\n",__FUNCTION__);
}
void printf_help(void)
{
    int cmd_at;
    printf("My shell commands:\n");
    for(cmd_at = 0; cmd_at <sizeof(CMD_TABLE)/sizeof(CMD_PARSE);cmd_at++ )
    {
        printf("CMD_TAB[%d]: %s-->%s\n", \
                cmd_at, CMD_TABLE[cmd_at].cmd_type, CMD_TABLE[cmd_at].cmd_help);
    }
}
void cmd_parse_exit(void)
{
    exit(0);
}

/*命令匹配*/
int command_matching(char *cmd_type)
{
    int cmd_at = 0;
    int cmd_match_flag = 0;
    int type_num = sizeof(CMD_TABLE)/sizeof(CMD_PARSE);

    for(cmd_at = 0; cmd_at < type_num; cmd_at++)
    {
        if(!strcmp(CMD_TABLE[cmd_at].cmd_type, cmd_type))
        {
            cmd_match_flag = 1;
            break;
        }
    }

    if(1 == cmd_match_flag)
    {
        cmd_match_flag = 0;
        CMD_TABLE[cmd_at].fun_ptr();
    }else{
        printf("no match cmd .\n");return -1;
    }

    return 1;
}

#define CMD_LINE_STR_LENGTH     (256)
void cmd_line(void)
{
    char str[CMD_LINE_STR_LENGTH]={0};
    while(1)
    {
        printf("fly#");
        memset(str, 0, sizeof(str));
        scanf("%s", str);
        command_matching(str);
    }
}

int main(int argc, char* argv[])
{
    cmd_line();
    return 0;
}
